home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 07511000 / var0888.dms / var0888.adf / WoManD / man / C_Library / qsort < prev    next >
Text File  |  1992-09-19  |  2KB  |  65 lines

  1. QSORT(1)                    C LIBRARY FUNCTIONS                     QSORT(1)
  2.  
  3. NAME
  4.      qsort - to quickly sort an array.
  5.  
  6. SYNOPSIS
  7.      qsort(const void *a_ptr, size_t n, size_t el_size,
  8.            int (*compar)(const void *, const void * ));
  9.  
  10. INCLUDE FILE
  11.      stdlib.h
  12.  
  13. DESCRIPTION
  14.      qsort() sorts the array pointed to by a_ptr in ascending order with
  15.            respect to the comparison function pointed to by compar. The
  16.            array  must have n elements, each one stored in el_size bytes. The
  17.            function compar() takes as  arguments  two  addresses of elements
  18.            of the array. It returns an int that  is less than,  equal to or
  19.            greater than zero, depending  on  whether the  element  pointed to
  20.            by its first argument is considered  to be less than,  equal to,
  21.            or greater than  the element pointed to by its second argument.
  22.            qsort() is an implementation of the quicker-sort  algorithm. It
  23.            sorts a table of data in place.
  24.  
  25. NOTES
  26.      The pointer to the base of the table should be of type
  27.      pointer-to-element, and cast to type pointer-to-void.
  28.  
  29.      The comparison function need not compare every byte, so arbitrary data
  30.      may be contained in the elements in addition to the values being
  31.      compared.
  32.  
  33. SEE ALSO
  34.      bsearch().
  35.  
  36. EXAMPLE
  37.      The following program sorts a simple array:
  38.  
  39.           static int intcompare(i,j)
  40.           int *i, *j;
  41.           { return(*i - *j);
  42.           }
  43.  
  44.           main()
  45.           { int a[10];
  46.             int i;
  47.  
  48.             a[0] = 9;
  49.             a[1] = 8;
  50.             a[2] = 7;
  51.             a[3] = 6;
  52.             a[4] = 5;
  53.             a[5] = 4;
  54.             a[6] = 3;
  55.             a[7] = 2;
  56.             a[8] = 1;
  57.             a[9] = 0;
  58.  
  59.             qsort(a,10,sizeof(int),intcompare)
  60.  
  61.             for (i=0; i<10; i++) printf(" %d",a[i]);
  62.  
  63.             printf("\n");
  64.           }
  65.